﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNA_TD.Shells
{
    public class MGTowerShell : IRunable, IFixedGameUpdate
    {
        GameObject target;
        float dmg;
        Vector3 position;

        public MGTowerShell(Vector3 pos, GameObject target, float dmg)
            : base(Priority.Effekt)
        {
            position = pos;
            this.dmg = dmg;
            this.target = target;
        }

        public void FixedGameUpdate()
        {
            var dir = position - target.position;
            if (dir.Length() < 0.2f)
            {
                target.Damage(dmg);
                UnRegister(this);
            }
            dir.Normalize();
            position -= dir * 0.05f;
            Register(new MGTowerShellEffect(position));
            position -= dir * 0.05f;
            Register(new MGTowerShellEffect(position));
            position -= dir * 0.05f;
            Register(new MGTowerShellEffect(position));
            position -= dir * 0.05f;
            Register(new MGTowerShellEffect(position));
        }

        public override void Load()
        {
        }

        public override void UnLoad()
        {
        }
    }

    public class MGTowerShellEffect : SquarePointSprite, IGameUpdate
    {
        long midTime;

        public MGTowerShellEffect(Vector3 pos)
            : base(pos, .1f, new Vector4(1, .5f, 0, 1), "Linear")
        {
            midTime = inGameTime;
        }

        public void GameUpdate()
        {
            var diff = 1 - (inGameTime - midTime) / 50f;
            if (diff <= 0)
            {
                UnRegister(this);
                return;
            }

            scale = diff;
            color.W = scale * .8f + .2f;
        }
    }
}
